package com.nuanshui.heatedloan.msgcenter.apppush.getui;

import com.alibaba.fastjson.JSON;
import com.gexin.rp.sdk.base.IPushResult;
import com.gexin.rp.sdk.base.impl.SingleMessage;
import com.gexin.rp.sdk.base.impl.Target;
import com.gexin.rp.sdk.base.payload.APNPayload;
import com.gexin.rp.sdk.exceptions.RequestException;
import com.gexin.rp.sdk.http.IGtPush;
import com.gexin.rp.sdk.template.APNTemplate;
import com.gexin.rp.sdk.template.NotificationTemplate;
import com.nuanshui.framework.utils.LogUtil;
import com.nuanshui.heatedloan.msgcenter.apppush.AppPush;
import com.nuanshui.heatedloan.msgcenter.apppush.bean.ThirdPartyPushResult;
import com.nuanshui.heatedloan.msgcenter.integration.AppPushMsgBody;
import com.nuanshui.heatedloan.msgcenter.integration.UserDeviceTokenVo;
import com.nuanshui.heatedloan.msgcenter.model.AppPushMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.Map;
import java.util.Set;

/**
 * 个推的正式环境推送
 *
 * @param
 * @author guohao
 * @date 2017年1月12日 上午11:44:11
 * @since $
 */
public class GeTuiAppPush implements AppPush {

    private static final Logger log = LoggerFactory.getLogger(GeTuiAppPush.class);

    private GeTuiConfig config;

    public GeTuiConfig getConfig() {
        return config;
    }

    public void setConfig(GeTuiConfig config) {
        this.config = config;
    }

    public GeTuiAppPush(GeTuiConfig config)
    {
         this.config=config;
    }

    @Override
    public String pushMessage(final AppPushMsgBody msg) {

        LogUtil.info(log, "用个推进行推送");

        String result = push(msg);
        msg.setResultMsg(result);
        return result;
    }

    private String push(final AppPushMsgBody msg) {

        if ("IOS".equals(msg.getDeviceToken().getDeviceType())) {
            IGtPush push = new IGtPush(config.getGeTuiUrl(), config.getGeTuiAppKey(), config.getGeTuiMasterSecret()); //进行推送的类
            APNTemplate t = new APNTemplate(); //推送的模板数据
            SingleMessage sm = new SingleMessage(); //单个推送类
            APNPayload.DictionaryAlertMsg alertMsg = new APNPayload.DictionaryAlertMsg();//推送的消息
            APNPayload apnpayload = new APNPayload();//推送的一些系统参数

            //设置自定义参数
            Set<String> keys = msg.getParams().keySet();
            for (String key : keys) {
                apnpayload.addCustomMsg(key, msg.getParams().get(key));
            }

            //设置具体推送的消息
            alertMsg.setTitleLocKey(msg.getMsgTitle());//设置头
            alertMsg.setBody(msg.getMsgContent());//设置显示的文本体

            //设置推送的一些参数
            apnpayload.setAlertMsg(alertMsg);
            apnpayload.addCustomMsg("badge", msg.getDeviceToken().getBadge());
            //apnpayload.addCustomMsg("badge",1);
            apnpayload.setSound("beep.wav");
            // apnpayload.setBadge(num);//这里先写死
            apnpayload.setBadge(1);

            //将参数封装到模板
            t.setAPNInfo(apnpayload);

            //将.........这是什么鬼，一层一层的
            sm.setData(t);

            //推送
            IPushResult ret0 = push.pushAPNMessageToSingle(config.getGeTuiAppId(), msg.getDeviceToken().getDeviceToken(), sm);
            System.out.println(ret0.getResponse());
            System.out.println(msg.getDeviceToken().getBadge());
            return JSON.toJSONString(ret0.getResponse());


        } else if ("Android".equals(msg.getDeviceToken().getDeviceType()) || "android".equals(msg.getDeviceToken().getDeviceType())) {
            IGtPush push = new IGtPush(config.getGeTuiUrl(), config.getGeTuiAppKey(), config.getGeTuiMasterSecret());

            NotificationTemplate template = new NotificationTemplate();
            // 设置APPID与APPKEY
            template.setAppId(config.getGeTuiAppId());
            template.setAppkey(config.getGeTuiAppKey());
            // 设置通知栏标题与内容
            template.setTitle(msg.getMsgTitle());
            template.setText(msg.getMsgContent());
            // 配置通知栏图标
            template.setLogo("push.png");
            // 配置通知栏网络图标，填写图标URL地址
            template.setLogoUrl("");
            // 设置通知是否响铃，震动，或者可清除
            template.setIsRing(true);
            template.setIsVibrate(true);
            template.setIsClearable(true);
            template.setTransmissionType(1);
            template.setTransmissionContent(JSON.toJSONString(msg.getParams()));

            SingleMessage message = new SingleMessage();
            message.setOffline(true);
            //离线有效时间，单位为毫秒，可选
            message.setOfflineExpireTime(24 * 3600 * 1000);
            message.setData(template);
            message.setPushNetWorkType(0); //可选。判断是否客户端是否wifi环境下推送，1为在WIFI环境下，0为不限制网络环境。
            Target target = new Target();
            target.setAppId(config.getGeTuiAppId());
            target.setClientId(msg.getDeviceToken().getDeviceToken());
            IPushResult ret = null;
            try {
                ret = push.pushMessageToSingle(message, target);
            } catch (RequestException e) {
                e.printStackTrace();
                ret = push.pushMessageToSingle(message, target, e.getRequestId());
            }
            if (ret != null) {
                return ret.getResponse().toString();
            } else {
                return "服务器响应异常";
            }
        }

        return "不支持的设置类型";
    }
}
